<?php
/**
 * Library Of Shared Code (LOSC)
 *
 * LICENSE
 *
 * This source file is subject to the New BSD License that is bundled
 * with this package in the file LICENSE.txt.
 *
 * @category   LOSC Framework
 * @package    View
 * @copyright  Copyright (c) 2008 Robin Skoglund (http://robinsk.net/)
 * @license    http://creativecommons.org/licenses/BSD/  New BSD License
 */

/**
 * Extends the ZF abstract view and adds commonly used helpers for performance
 *
 * @category   LOSC Framework
 * @package    View
 * @copyright  Copyright (c) 2008 Robin Skoglund (http://robinsk.net/)
 * @license    http://creativecommons.org/licenses/BSD/  New BSD License
 */
class Losc_View_ExtendedView extends Zend_View_Abstract
{
    /**
     * Translator
     *
     * @var Zend_Translate_Adapter
     */
    private $_translator;

    /**
     * Sets a translation Adapter for translation
     *
     * @param  Zend_Translate|Zend_Translate_Adapter $translate Instance of Zend_Translate
     * @throws Zend_View_Exception When no or a false instance was set
     * @return Zend_View_Helper_Translate
     */
    public function setTranslator($translator)
    {
        if ($translator instanceof Zend_Translate_Adapter) {
            $this->_translator = $translator;
        } else if ($translator instanceof Zend_Translate) {
            $this->_translator = $translator->getAdapter();
        } else {
            $msg = '$translator must be an instance of '
                 . 'Zend_Translate or Zend_Translate_Adapter';
            throw new Zend_View_Exception($msg);
        }

        return $this;
    }

    /**
     * Retrieve translation object
     *
     * If none is currently registered, attempts to pull it from the registry
     * using the key 'Zend_Translate'.
     *
     * @return Zend_Translate_Adapter|null
     */
    public function getTranslator()
    {
        if ($this->_translator === null) {
            if (Zend_Registry::isRegistered('Zend_Translate') === true) {
                $this->setTranslator(Zend_Registry::get('Zend_Translate'));
            }
        }

        return $this->_translator;
    }
    
    /**
     * Generates an url given the name of a route.
     *
     * @access public
     *
     * @param  array $urlOptions Options passed to the assemble method of the Route object.
     * @param  mixed $name The name of a Route to use. If null it will use the current Route
     * @param  bool $reset Whether or not to reset the route defaults with those provided
     * @return string Url for the link href attribute.
     */
    public function url(array $urlOptions = array(), $name = null, $reset = false, $encode = true)
    {
        $router = Zend_Controller_Front::getInstance()->getRouter();
        return $router->assemble($urlOptions, $name, $reset, $encode);
    }

    /**
     * Translates a message
     *
     * @param  string $messageId id of the message to be translated
     * @return string
     */
    public function translate($messageId)
    {
        $substitutions = func_get_args();
        array_shift($substitutions);

        if (count($substitutions) == 1 && is_array($substitutions[0])) {
            $substitutions = $substitutions[0];
        }
        
        if ($translator = $this->getTranslator()) {
            $message = $translator->translate($messageId);
        } else {
            $message = $messageId;
        }
        
        if (count($substitutions) > 0) {
            $message = vsprintf($message, $substitutions);
        }

        return $message;
    }

    /**
     * Includes the view script in a scope with only public $this variables
     *
     * @param  string  the view script to execute
     * @return mixed
     */
    protected function _run()
    {
        include func_get_arg(0);
    }
}
